<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\MettingGuest
 *
 * @property int $id 主键
 * @property int $groupid 分组
 * @property string|null $name 姓名
 * @property string|null $tel 电话
 * @property string|null $organization 组织单位
 * @property string $image 照片
 * @property string|null $code 邀请码
 * @property int $hotel_id 入住酒店
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\MeetingGroup $group
 * @property-read \App\Models\MeetingHotel $hotel
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereGroupid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereHotelId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereOrganization($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereTel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MeetingGuest whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class MeetingGuest extends Model
{
    protected $table = 'meeting_guest';
    protected $primaryKey = 'id';
    protected $fillable = ['name', 'tel', 'image', 'code', 'organization', 'groupid', 'hotel_id'];
    protected $with = ['group', 'hotel'];

    /**
     * @param $value
     * @return string
     */
    public function getImageAttribute($value)
    {
        return $value ? image_url($value) : $value;
    }

    /**
     * @param $value
     */
    public function setImageAttribute($value)
    {
        $this->attributes['image'] = strip_image_url($value);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function group()
    {
        return $this->belongsTo(MeetingGroup::class, 'groupid', 'groupid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function hotel()
    {
        return $this->belongsTo(MeetingHotel::class, 'hotel_id', 'id');
    }

    /**
     * @param $code
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public static function findByCode($code)
    {
        return MeetingGuest::where('code', $code)->first();
    }
}
